//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.IO;
using CommunityServer.Components;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Summary description for MPContainer.
	/// </summary>
	public class MPContainer : MetaBuilders.WebControls.MasterPages.ContentContainer
	{
		public MPContainer()
		{

		}

		/// <summary>
		/// Override Onit to enable setting the MasterPageFile value to a known CS Theme
		/// </summary>
		/// <param name="e"></param>
		protected override void OnInit(EventArgs e)
		{
			if(this.MasterPageFile == null)
			{
				if(ThemeMasterExists)
					this.MasterPageFile = ThemePath;
				else if(DefaultMasterExists)
					this.MasterPageFile = DefaultThemePath;
				else
					throw new Exception(string.Format("The MasterThemeFile {0} could not be found in the {1} or default theme directory",ThemeMasterFile,ThemeName));
			}
			base.OnInit (e);
		}

		private string _themeMasterFile = "master.ascx";

		/// <summary>
		/// Specifies the name of the theme file. Expects to find this file in the Masters directory
		/// </summary>
		public virtual string ThemeMasterFile
		{
			get
			{
				return _themeMasterFile;
			}
			set
			{
				_themeMasterFile = value;
			}
		}

		private bool _isModal = false;
		public virtual bool IsModal
		{
			get
			{
				return _isModal;
			}
			set
			{
				_isModal = value;
				CSContext.Current.IsModal = true;
			}
		}

		private bool ThemeMasterExists
		{
			get
			{
				string filename = Context.Server.MapPath(ThemePath);
				return File.Exists(filename);
			}
		}

		private bool DefaultMasterExists
		{
			get
			{
				return File.Exists(Context.Server.MapPath(DefaultThemePath));
			}
		}


		/// <summary>
		/// Folder which contains the master files
		/// </summary>
		private string themeFolder = null;
		public virtual string ThemeFolder 
		{
			get 
			{
				if(themeFolder == null)
					return "~/Themes/" + ThemeName + "/Masters/"; //TODO Use correct skin folder
				else
					return themeFolder;
			}
			set
			{
				themeFolder = value;
			}
		}

		/// <summary>
		/// Full path to the master file
		/// </summary>
		private String ThemePath 
		{
			get 
			{
				return this.ThemeFolder + ThemeMasterFile;
			}
		}

		/// <summary>
		/// Current user selected theme
		/// </summary>
		protected virtual string ThemeName 
		{
			get 
			{
				return CSContext.Current.User.Theme;
			}
		}

		protected virtual string DefaultThemePath 
		{
			get 
			{
				return "~/Themes/default/Masters/" + ThemeMasterFile;
			}
		}


		#region Dozing Dogs additions 
		private ArrayList _scripts = null;
		private string sTitle, sEnctype, sOnBeforeUnload, sOnSubmit;
		private bool bHide, bCountAsPage;

		/// <summary>
		/// The title of the page, i.e. <title></title>
		/// </summary>
		public string Title
		{
			get {return sTitle;}
			set {sTitle=value;}
		}

		/// <summary>
		/// hide page from robots (NOINDEX, NOFOLLOW)
		/// </summary>
		public bool HideFromRobots
		{
			get {return bHide;}
			set {bHide=value;}
		}

		/// <summary>
		/// is this a real page that should increment the page count?
		/// </summary>
		public bool CountAsPage
		{
			get {return bCountAsPage;}
			set {bCountAsPage=value;}
		}

		/// <summary>
		/// form enctype
		/// </summary>
		public string Enctype
		{
			get {return sEnctype;}
			set {sEnctype=value;}
		}

		/// <summary>
		/// body unbeforeunload script
		/// </summary>
		public string OnBeforeUnload
		{
			get {return sOnBeforeUnload;}
			set {sOnBeforeUnload=value;}
		}

		/// <summary>
		/// form onsubmit code
		/// </summary>
		public string OnSubmit
		{
			get {return sOnSubmit;}
			set {sOnSubmit=value;}
		}

		public ArrayList Scripts
		{
			get {return _scripts;}
		}
		#endregion

	}




	public class MPRegion : MetaBuilders.WebControls.MasterPages.Region{}
	public class MPContent : MetaBuilders.WebControls.MasterPages.Content{}
	public class MPForm : MetaBuilders.WebControls.MasterPages.NoBugForm {}

	/// <summary>
	/// This control contains optional javascript for the page
	/// </summary>
	public class MPScript : System.Web.UI.WebControls.PlaceHolder {}
}
